<?php
/**
 * Class: Admin_SchoolController
 * This class governs the addition of new schools to the system.
 * @author ovo
 *
 */
class Admin_SchoolController extends Zend_Controller_Action
{

	protected $_school;
	
    public function init()
    {
        /* Initialize action controller here */
    	$this->_school = new Admin_Model_DbTable_School();
    }

    public function indexAction()
    {
        // action body
    }

    public function addschoolAction()
    {
    	$this->view->title = "Add a school";
    	$form = new Admin_Form_addSchool();
    	$this->view->form = $form;
    	if ($this->getRequest()->isPost()){
    		$formData = $this->getRequest()->getPost();
    		if($form->isValid($formData)){
        	$schoolName = $form->getValue('school');
        	  $this->_school->addSchool($schoolName);
    			$this->view->message = "$schoolName added to database";
    		}else{
    			$form->populate($formData);

    		}

    	}
    }

    /**
     * Function: ViewAllSchools
     * This method returns all schools registered in the database
     * 
     * 
     */
    public function viewschoolsAction()
    {
    	$school = new Admin_Model_DbTable_School();
        $this->view->schools = $school->getSchools();
        
    }

    public function getdeptsAction() {
    	
    	$school = new Admin_Model_DbTable_School();
    	$del =  $school->find(7);
    	$del = $del->current();
		$this->view->aaa = $del->findDependentRowset('Admin_Model_DbTable_schooldept');
    }

    public function addDeptSchoolAction() {
    	
        $this->view->title = "Associate a Department to a School";
    	$form = new Admin_Form_schooldept();
    	$this->view->form = $form;
    	
    	if ($this->getRequest()->isPost()){
    	$formData = $this->getRequest()->getPost();
    
    		if ($form->isValid($formData)){
    			
    			//$schoolid = $form->getValue('school');
    			$school = $form->getValue('school');
    			$dept = $form->getValue('dept');
    			$enterDept = new Admin_Model_DbTable_schooldept();
    			$enterDept->schoolDepts($school, $dept);
    			$this->view->message = "$dept has been associated with $school";
    		}
    	}
    	
    }
        
    /**
    }

    /**
     * Function: removeschool
     * This function removes a particular school from the database.
     * 
     */
    public function removeschoolAction()
    {
    	$id = $this->getRequest()->getParam('id');
        $school = new Admin_Model_DbTable_School();
    	$school->deleteSchool($id);
    	$this->_redirect('/admin/school/viewschools');
        
    	//$this->view->message = "School of $id has been deleted";    
    }
    

    /**
     * This function updates a particular school information
     */
    public function updateschoolAction()
    {
        // action body
        $this->view->title = "Edit a school";
        $form = new Admin_Form_addSchool();
        $form->submit->setLabel('Edit');
        $this->view->form = $form;
        
		 if ($this->getRequest()->isPost()) {
				$formData = $this->getRequest()->getPost();
				if ($form->isValid($formData)) {
				$id = (int)$form->getValue('id');
				$name = $form->getValue('school');
				$school = new Admin_Model_DbTable_School();
				$school->updateschool($id, $name);
				$this->_redirect('/admin/school/viewschools');
		} else {
				$form->populate($formData);
		}
		} else {
		$id = $this->getRequest()->getParam('id');
		if ($id > 0) {
		$school = new Admin_Model_DbTable_School();
		$data =$school->getSchool($id);
		$form->populate($data);
		//print_r($data);
		}
}
        
    }


}








